// pages/addAddr/addAddr.js
var utils = require('../../utils/util.js');
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        inputValueName: '',
        inputValuePhone: '',
        inputValueDetail: '',
        region: ['贵州省', '贵阳市', '花溪区'],
        addressInfo: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.addressID != '' && options.addressID != null) {
            wx.setNavigationBarTitle({
                title: '修改地址'
            })
            this.setData({
                addressID: options.addressID
            })
            this.getAddressInfo(options.addressID);
        } else {
            this.setData({
                addressID: ''
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    getAddressInfo: function (_addressID) {
        var that = this;
        var params = {
            userID: app.globalData.userID,
            addressID: _addressID
        }
        app.network.requestPost(app.route.ROUTE_GET_ADDRESS_INFO, params, '正在获取数据', function (res) {
            //res就是我们请求接口返回的数据
            if (res.success) {
                var _addressInfo = res.context.address;
                that.setData({
                    inputValueName: _addressInfo.consigneeName,
                    inputValuePhone: _addressInfo.consigneePhone,
                    inputValueDetail: _addressInfo.campus,
                    region: [_addressInfo.provinceName, _addressInfo.cityName, _addressInfo.countyName]
                });
            } else {
                wx.showToast({
                    title: res.msg,
                })
            }
        }, function (res) {
            wx.showToast({
                title: '加载数据失败',
            })
        })
    },
    bindInputName: function (e) {
        this.setData({
            inputValueName: e.detail.value
        })
    },
    bindInputPhone: function (e) {
        this.setData({
            inputValuePhone: e.detail.value
        })
    },
    bindPickerAddrChange(e) {
        console.log(e);
        this.setData({
            region: e.detail.value
        })
    },
    bindInputDetail(e) {
        this.setData({
            inputValueDetail: e.detail.value
        })
    },
    submitAddr: function(e) {
        var that = this;
        if (that.data.inputValueName == null || that.data.inputValueName == '') {
            wx.showToast({
                title: '请填写姓名'
            })
            return;
        }
        if (!utils.testPhone(that.data.inputValuePhone)) {
            wx.showToast({
                title: '电话号码错误'
            })
            return;
        }
        if (that.data.inputValueDetail == null || that.data.inputValueDetail == "") {
            wx.showToast({
                title: '请填写详细地址'
            })
            return;
        }
        if (that.data.addressID != '' && that.data.addressID != null) {
            var params = {
                userID: app.globalData.userID,
                addressID: that.data.addressID,
                provinceName: that.data.region[0],
                cityName: that.data.region[1],
                countyName: that.data.region[2],
                consigneeName: that.data.inputValueName,
                consigneePhone: that.data.inputValuePhone,
                campus: that.data.inputValueDetail
            }
        } else {
            var params = {
                userID: app.globalData.userID,
                provinceName: that.data.region[0],
                cityName: that.data.region[1],
                countyName: that.data.region[2],
                consigneeName: that.data.inputValueName,
                consigneePhone: that.data.inputValuePhone,
                campus: that.data.inputValueDetail
            }
        }
        console.log(params);
        app.network.requestPost(app.route.ROUTE_SAVE_ADDRESS, params, '正在保存', function (res) {
            //res就是我们请求接口返回的数据
            if (res.success) {
                wx.showToast({
                    title: '保存成功'
                })
                setTimeout(function () {
                    wx.navigateBack({
                        delta: 1,
                    })
                }, 1000)
            } else {
                wx.showToast({
                    title: res.msg,
                })
            }
        }, function (res) {
            wx.showToast({
                title: '加载数据失败',
            })
        })
    }
})